package com.net.xpay.common.helper;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.net.xpay.common.manager.CommonSysConfigManager;
import com.net.common.util.SerializationUtil;
import com.net.xpay.common.domain.CommonSysConfig;
import com.net.xpay.common.enums.config.CommonSysConfigKey;
import com.net.xpay.common.enums.config.ValueType;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author  on 27/04/2018.
 */
@Component
public class CommonSysConfigHelper implements InitializingBean {
    @Autowired
    private CommonSysConfigManager commonSysConfigManager;

    private final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1,
            new ThreadFactoryBuilder().setDaemon(true).setNameFormat("common-sys-config-schedule").build());

    private volatile Map<String, CommonSysConfig> sysConfigMap = new ConcurrentHashMap<>();

    @Override
    public void afterPropertiesSet() {
        executor.scheduleWithFixedDelay(this::init, 1, 10, TimeUnit.SECONDS);
    }

    public Long getLong(CommonSysConfigKey sysConfigKey) {
        CommonSysConfig sysConfig = sysConfigMap.get(sysConfigKey.getValue());
        if (sysConfig == null) {
            return null;
        } else if (sysConfig.getValueType() != ValueType.NUMBER) {
            throw new IllegalArgumentException("common sys config key的value类型不正确|key=" + sysConfigKey.getValue() + "|valueType=" + sysConfig.getValueType().getValue());
        }
        return Long.valueOf(sysConfig.getValue());
    }

    public String getString(CommonSysConfigKey sysConfigKey) {
        CommonSysConfig sysConfig = sysConfigMap.get(sysConfigKey.getValue());
        if (sysConfig == null) {
            return null;
        } else if (sysConfig.getValueType() != ValueType.STRING) {
            throw new IllegalArgumentException("common sys config key的value类型不正确|key=" + sysConfigKey.getValue() + "|valueType=" + sysConfig.getValueType().getValue());
        }
        return sysConfig.getValue();
    }


    public List<String> getStringArr(CommonSysConfigKey sysConfigKey) {
        CommonSysConfig sysConfig = sysConfigMap.get(sysConfigKey.getValue());
        if (sysConfig == null) {
            return null;
        } else if (sysConfig.getValueType() != ValueType.STRING_ARR) {
            throw new IllegalArgumentException("common sys config key的value类型不正确|key=" + sysConfigKey.getValue() + "|valueType=" + sysConfig.getValueType().getValue());
        }
        return SerializationUtil.str2Object(sysConfig.getValue(), new TypeReference<List<String>>() {});
    }

    private synchronized void init() {
        List<CommonSysConfig> sysConfigList = commonSysConfigManager.listAll();
        Map<String, CommonSysConfig> map = new ConcurrentHashMap<>(64);
        sysConfigList.forEach(sysConfig -> map.put(sysConfig.getKey(), sysConfig));
        sysConfigMap = map;
    }
}
